package com.ydgg.ltar.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ydgg.ltar.api.ApiResult;
import com.ydgg.ltar.mapper.AreaGiftMapper;
import com.ydgg.ltar.mapper.AreaListMapper;
import com.ydgg.ltar.mapper.AreaMapper;
import com.ydgg.ltar.mapper.ChartsMapper;
import com.ydgg.ltar.mapper.MemberGiftMapper;
import com.ydgg.ltar.mapper.MemberMapper;
import com.ydgg.ltar.model.Area;
import com.ydgg.ltar.model.AreaExample;
import com.ydgg.ltar.model.AreaGift;
import com.ydgg.ltar.model.AreaGiftExample;
import com.ydgg.ltar.model.AreaList;
import com.ydgg.ltar.model.AreaListExample;
import com.ydgg.ltar.model.Charts;
import com.ydgg.ltar.model.ChartsExample;
import com.ydgg.ltar.model.Member;
import com.ydgg.ltar.model.MemberGift;
import com.ydgg.ltar.model.MemberGiftExample;
import com.ydgg.ltar.util.GisUtil;
import com.ydgg.ltar.util.PageInfo;

/**
 * 排行榜
 * 作者： 张叶
 * 创建时间：2019年3月7日  下午1:54:23
 */
@Service
public class ChartsService {

	@Autowired
	private ChartsMapper chartsMapper;
	
	@Autowired
	private MemberMapper memberMapper;
	
	@Autowired
	private AreaGiftMapper areaGiftMapper;
	
	@Autowired
	private AreaMapper areaMapper;
	
	@Autowired
	private AreaListMapper areaListMapper;
	
	@Autowired
	private MemberGiftMapper memberGiftMapper;
	
	
	/**
	 * 添加或更新排行
	 * 作者： 张叶
	 * 创建时间：2019年3月15日  下午4:16:53
	 */
	@Transactional
	public ApiResult getGiftByGrade(String openId,String nickName,String imageUrl,double grade,String lat,String lng){
		ApiResult result = new ApiResult();
		Charts c = new Charts();
		//没有会员，新建会员
		Member m = memberMapper.findByOpenId(openId);
		if(m == null){//如果没有该会员，添加此会员
			m = new Member();
			m.setOpenId(openId);
			m.setNickName(nickName);
			m.setImageUrl(imageUrl);
			m.setCreatedAt(new Date());
			m.setStatus("on");
			memberMapper.insert(m);
		}
		
		//刷新排行榜
		ChartsExample example1 = new ChartsExample();
		example1.createCriteria().andStatusNotEqualTo("deleted").andMemberIdEqualTo(m.getId());
		List<Charts> list = chartsMapper.selectByExample(example1); 
		if(list.size() > 0){
			//判断成绩是否高于之前
			c = list.get(0);
			if(c.getGrade() > grade){
				c.setUpdateDate(new Date());
				c.setGrade(grade);
				chartsMapper.updateByPrimaryKeySelective(c);
			}
		}else{
			c.setMemberId(m.getId());
			c.setUpdateDate(new Date());
			c.setStatus("on");
			c.setCreatedAt(new Date());
			c.setGrade(grade);
			chartsMapper.insert(c);
		}

		//判断是否已获取礼物 
		MemberGiftExample mgexample = new MemberGiftExample();
		mgexample.createCriteria().andStatusEqualTo("on").andMIdEqualTo(m.getId());
		List<MemberGift> mgList =  memberGiftMapper.selectByExample(mgexample);
		if(mgList.size() == 0){
			GisUtil gu = new GisUtil();
			//所有商家
			AreaExample example = new AreaExample();
			example.createCriteria().andStatusEqualTo("on");
			List<Area> lists = areaMapper.selectByExample(example);
			AreaListExample alexample = new AreaListExample();
			List<AreaList> alist = new ArrayList<AreaList>();
			Long aId = 0L;
			for(Area a: lists){//遍历商家
				alexample.createCriteria().andAreaIdEqualTo(a.getId()).andStatusEqualTo("on");
				alist = areaListMapper.selectByExample(alexample);//商家区域
				if(gu.getRange(alist, Double.parseDouble(lng),Double.parseDouble(lat))){
					aId = a.getId();
					break;
				}
			}
			//查看礼品
			AreaGiftExample agExample = new AreaGiftExample();
			AreaGiftExample.Criteria criteria = agExample.createCriteria();
			criteria.andStatusEqualTo("on");
			if(aId != 0){
				criteria.andAreaIdEqualTo(aId);
			}else{
				criteria.andAreaIdIsNull();
			}
			List<AreaGift> agList = areaGiftMapper.selectByExample(agExample);
			if(agList.size() > 0){
				AreaGift ag = agList.get(getRandom(agList.size()) - 1);
				//添加member_gift表
				MemberGift mg = new MemberGift();
				mg.setAgId(ag.getId());
				mg.setmId(m.getId());
				mg.setCreatedAt(new Date());
				mg.setStatus("on");
				memberGiftMapper.insertSelective(mg);
				result.data.put("gift", ag);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 3;
				result.msg.desc = "该区域暂无礼物";
			}
		}else{
			AreaGift ag = areaGiftMapper.selectByPrimaryKey(mgList.get(0).getAgId());
			result.data.put("gift", ag);
			result.msg.status = 4;
			result.msg.desc = "已获取礼品";
		}
		
		return result;
	}
	
	
//	/**
//	 * 添加或更新排行
//	 * 作者： 张叶
//	 * 创建时间：2019年3月15日  下午4:16:53
//	 */
//	@Transactional
//	public ApiResult getGiftByGrade(String openId,String nickName,String imageUrl,double grade,String lat,String lng){
//		ApiResult result = new ApiResult();
//		Charts c = new Charts();
//		Member m = memberMapper.findByOpenId(openId);
//		if(m == null){//如果没有该会员，添加此会员
//			m = new Member();
//			m.setOpenId(openId);
//			m.setNickName(nickName);
//			m.setImageUrl(imageUrl);
//			m.setCreatedAt(new Date());
//			m.setStatus("on");
//			memberMapper.insert(m);
//		}
//		MemberGiftExample mgexample = new MemberGiftExample();
//		mgexample.createCriteria().andStatusEqualTo("on").andMIdEqualTo(m.getId());
//		List<MemberGift> mgList =  memberGiftMapper.selectByExample(mgexample);
//		if(mgList.size() == 0){//已得礼物 
//			String where = " and status != 'deleted' and member_id = " + m.getId();
//			List<Charts> list = chartsMapper.selectByWhere(where); 
//			int i = 0;
//			if(list.size() > 0){
//				c = list.get(0);
//				c.setUpdateDate(new Date());
//				c.setGrade(grade);
//				i = chartsMapper.updateByPrimaryKeySelective(c);
//			}else{
//				c.setMemberId(m.getId());
//				c.setUpdateDate(new Date());
//				c.setStatus("on");
//				c.setCreatedAt(new Date());
//				c.setGrade(grade);
//				i = chartsMapper.insert(c);
//			}
//			if(i > 0){
//				GisUtil gu = new GisUtil();
//				//所有商家
//				AreaExample example = new AreaExample();
//				example.createCriteria().andStatusEqualTo("on");
//				List<Area> lists = areaMapper.selectByExample(example);
//				AreaListExample alexample = new AreaListExample();
//				List<AreaList> alist = new ArrayList<AreaList>();
//				Long aId = 0L;
//				for(Area a: lists){//遍历商家
//					alexample.createCriteria().andAreaIdEqualTo(a.getId()).andStatusEqualTo("on");
//					alist = areaListMapper.selectByExample(alexample);//商家区域
//					if(gu.getRange(alist, Double.parseDouble(lng),Double.parseDouble(lat))){
//						aId = a.getId();
//						break;
//					}
//				}
//				//查看礼品
//				AreaGiftExample agExample = new AreaGiftExample();
//				AreaGiftExample.Criteria criteria = agExample.createCriteria();
//				criteria.andStatusEqualTo("on");
//				if(aId != 0){
//					criteria.andAreaIdEqualTo(aId);
//				}else{
//					criteria.andAreaIdIsNull();
//				}
//				List<AreaGift> agList = areaGiftMapper.selectByExample(agExample);
//				if(agList.size() > 0){
//					AreaGift ag = agList.get(getRandom(agList.size()) - 1);
//					//添加member_gift表
//					MemberGift mg = new MemberGift();
//					mg.setAgId(ag.getId());
//					mg.setmId(m.getId());
//					mg.setCreatedAt(new Date());
//					mg.setStatus("on");
//					memberGiftMapper.insertSelective(mg);
//					result.data.put("gift", ag);
//					result.msg.status = 0;
//					result.msg.desc = "获取成功";
//				}else{
//					result.msg.status = 3;
//					result.msg.desc = "该区域暂无礼物";
//				}
//			}else{
//				result.msg.status = 1;
//				result.msg.desc = "系统错误，操作失败";
//			}
//		}else{
//			AreaGift ag = areaGiftMapper.selectByPrimaryKey(mgList.get(0).getAgId());
//			result.data.put("gift", ag);
//			result.msg.status = 4;
//			result.msg.desc = "已获取礼品";
//		}
//		return result;
//	}

	
	private int getRandom(int size){
		int i = (int)(1 + Math.random() * size);
		return i;
	}
	
	
	public List<Charts> selectByWhere(String where){
		List<Charts> list = chartsMapper.selectByWhere(where);
		return list;
	}
	
	
	public List<Map<String,Object>> selectByPage(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = chartsMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	
	
	public int update(Charts c){
		int i = chartsMapper.updateByPrimaryKey(c);
		return i ;
	}
	
	public int insert(Charts c){
		int i = chartsMapper.insert(c);
		return i ;
	}
	
	public List<Map<String,Object>> selectByTop(){
		List<Map<String,Object>> list = chartsMapper.selectByTop();
		return list;
	}
}
